#pragma once
#include "PrintPanel.h"


PrintPanel::PrintPanel(HINSTANCE hInstance, char * lpszClassName)
	:ContainerPanel(hInstance, lpszClassName)
{	

	topPrintPanel = this;
	numberOfFirstFrame = 0;
	numberOfLastFrame = 0;
	rows = 1;
	cols = 1;	
	isViewedOne = false;
	firstRepaint = false;	
	if(firstDicomFile == NULL)
		firstDicomFile = new DICOMFile();		
	 if(strncmp(lpszClassName, "print", 10) == 0)
		indication = QString("print");
	 else
		indication = QString("empty");	

}

void PrintPanel::addDicomStructs(){	
	Panel::isNeededUpdateImages = true;
	while(true){
		if(!addImagePanel((DICOMStruct *)NULL)){
			clearNullImages();
			break;	
		}
	}
	Panel::isNeededUpdateImages = false;
	DICOMStruct * dicomStruct;
	std::list <DICOMStruct *>::iterator iter;			
	 iter = Panel::dicomStructsToPrint.begin();
	int i = 0;
	for(; iter != dicomStructsToPrint.end(); iter++){
		dicomStruct = *iter;
		if( i >= numberOfFirstFrame){
			if(!addImagePanel(dicomStruct)){
				clearNullImages();
				break;			
			}
		}
		numberOfLastFrame = i;
		i++;
	}
	
	
	
}

void PrintPanel::addDicomStructsWithUpdateSeries(){
	addDicomStructs();
	clearNullImages();
}

void PrintPanel::addDicomStructsWithAddNewSeries(){
	addDicomStructs();
	clearNullImages();
}

void PrintPanel::ViewOne(Panel * currentViewedPanel){

}

void PrintPanel::SetColsRows(int cols, int rows){		
	flushViewedOne();
	if(!((PrintPanel *)getTopTree())->isViewedOne){
		this->cols = cols;	
		this->rows = rows;
		reSizeChildren();	
		prevNumberOfFirstFrame = 0;
		numberOfFirstFrame = 0;
		addDicomStructs();
		((PrintPanel *)getTopTree())->addDicomStructs();
		clearNullImagesAll();
		getTopTree()->updateMainWindow();
	}
	return;
}	

void PrintPanel::nextPage(){
	if(dicomStructsToPrint.size() == 0)
		return;
	Panel::isNeededUpdateImages = true;
	int saveNumberOfFirstFrame = numberOfFirstFrame;	
	if(numberOfLastFrame + 1 >  dicomStructsToPrint.size() - 1)
		numberOfLastFrame = dicomStructsToPrint.size() - 1;
	else
		numberOfFirstFrame = numberOfLastFrame + 1;
	if(saveNumberOfFirstFrame != numberOfFirstFrame)
		prevNumberOfFirstFrame = saveNumberOfFirstFrame;
	addDicomStructs();
	repaintImage();
	Panel::isNeededUpdateImages = false;
}


void PrintPanel::prevPage(){
	int a = numberOfFirstFrame - prevNumberOfFirstFrame;
	numberOfFirstFrame = prevNumberOfFirstFrame;	
	if(prevNumberOfFirstFrame < 0){
		numberOfFirstFrame = 0;
		prevNumberOfFirstFrame = 0;
	}
	prevNumberOfFirstFrame = numberOfFirstFrame - a;
	addDicomStructs();
	repaintImage();	
}


LRESULT PrintPanel::WndProc_(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	Panel::WndProc_(hWnd, message, wParam, lParam);
	if(isFrozen)
		return true;
	//HDC hdc;	
	switch (message){
		//	case WM_ERASEBKGND:{
		//		if(this->parent != NULL)
		//			return TRUE;	
		//	}
	case WM_SETCURSOR:{
		::SetCursor(LoadCursorA(hInst,  MAKEINTRESOURCE(IDC_POINTER)));		
	}
	case WM_KEYDOWN:{
		if(wParam == 16)//press Shift
			Panel::shifted = true;
		if(wParam == 33){//press PageUp		
			setQuickly(1000);			
			((ContainerPanel *)Panel::activeTopTreePanel)->goNextAllImages(-1);	
		}
		if(wParam == 34){//press PageDown				
			setQuickly(1000);
			((ContainerPanel *)Panel::activeTopTreePanel)->goNextAllImages(1);	
		}
		break;
	}

	case WM_KEYUP:{
		if(wParam == 16){			
			Panel::shifted = false;
		}
		break;
				  }
	case WM_SIZE:{
		reSizeChildren();	
		break;
				 }
	case WM_CREATE:{
		on_wm_create(hWnd, message, wParam, lParam);
		break;
				   }	
	case WM_DESTROY:{
		on_wm_destroy(hWnd, message, wParam, lParam);	
		//PostQuitMessage(1);
		//return 1;
		break;
					}	
	case WM_LBUTTONUP:{		
		on_wm_lrbuttonup(hWnd, message, wParam, lParam);		
		break;
					  }
	case WM_LBUTTONDOWN:{
		on_wm_lrbuttondown(hWnd, message, wParam, lParam);		
		break;
						}
	case WM_RBUTTONDOWN:{
		on_wm_lrbuttondown(hWnd, message, wParam, lParam);		
		break;	
						}	
	case WM_MOUSEMOVE:{	
		//on_wm_mousemove(hWnd, message, wParam, lParam);	
		break;
					  }
	case WM_PAINT:{		
		on_wm_paint(hWnd, message, wParam, lParam);		
		break;
				  }
				  //	case WM_DESTROY:
				  //		PostQuitMessage(0);
				  //		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}


void PrintPanel::on_wm_lrbuttondown(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){

}